#include "server.h"

void sqliteinsert(TEMP *a, DATA *b) //注册用 数据库写入操作
{
    TEMP temp = *a;
    DATA data = *b;
    char sql[256] = {0};
    char *errmsg;
    char **dbResult;
    int nRow = 0, nColumn = 0;

    sprintf(sql, "select score from %s where name='%s'", UDBNAME, data.name);

    sqlite3_get_table(temp.db, sql, &dbResult, &nRow, &nColumn, &errmsg);

    if (nRow != 0) //用户名已被使用
    {
        memset(data.text1, 0, sizeof(data.text1));
        strcpy(data.text1, "n");
        send(temp.fd, &data, sizeof(DATA), 0);
        printf("logeout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
               data.flags, data.num, data.name, data.text1, data.text2, data.text3);
    }
    else //用户名可用
    {
        memset(&sql, 0, sizeof(sql));
        if (data.num == 95)
        {
            sprintf(sql, "insert into  %s values('%s','%s','%s','%s',%d)",
                    UDBNAME, data.name, data.text1, data.text2, data.text3, 1);
        }
        else
        {
            sprintf(sql, "insert into  %s values('%s','%s','%s','%s',%d)",
                    UDBNAME, data.name, data.text1, data.text2, data.text3, 0);
        }
        if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) == SQLITE_OK)
        {
            memset(data.text1, 0, sizeof(data.text1));
            strcpy(data.text1, "y");
            send(temp.fd, &data, sizeof(DATA), 0);
            printf("logyout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                   data.flags, data.num, data.name, data.text1, data.text2, data.text3);
        }
        else
        {
            printf("%s\n", errmsg);
            memset(data.text1, 0, sizeof(data.text1));
            strcpy(data.text1, "n");
            send(temp.fd, &data, sizeof(DATA), 0);
            printf("lognout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                   data.flags, data.num, data.name, data.text1, data.text2, data.text3);
        }
    }
}

void sqliteget(TEMP *a, DATA *b) //查询库 登录行为
{
    TEMP temp = *a;
    DATA data = *b;
    char name[32] = {0};
    char sql[128] = {0};
    char time[32] = {0};
    char *errmsg;
    char **dbResult;
    int nRow = 0, nColumn = 0;
    int clientnum = 0;
    if (data.num == 2)
    {
        timeget(time);
        sprintf(sql, "insert into %s values('%s', '%s', '%s', '%s')", HDBNAME, time, data.name, "logout", "Y");
        if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
        {
            printf("录入数据库失败：%s\n", errmsg);
        }
        else
        {
            printf("操作已录入数据库\n");
        }
    }
    else
    {
        sprintf(sql, "select * from %s where name='%s' and password='%s'", UDBNAME, data.name, data.text1);
        sqlite3_get_table(temp.db, sql, &dbResult, &nRow, &nColumn, &errmsg);
        printf("nRow=%d\n", nRow);
        if (nRow != 0) //密码表中有该用户
        {
            memset(data.text1, 0, sizeof(data.text1));
            strcpy(data.text1, "y");
            data.num = data.num = (*dbResult[4 + nColumn]) - 48;
            send(temp.fd, &data, sizeof(DATA), 0);
            printf("inyout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                   data.flags, data.num, data.name, data.text1, data.text2, data.text3);

            timeget(time);
            sprintf(sql, "insert into %s values('%s', '%s', '%s', '%s')", HDBNAME, time, data.name, "login", "Y");
            if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
            {
                printf("录入数据库失败：%s\n", errmsg);
            }
            else
            {
                printf("操作已录入数据库\n");
            }
        }
        /******登录完成********/
        else //密码表中无该用户
        {
            memset(data.text1, 0, sizeof(data.text1));
            strcpy(data.text1, "n");
            send(temp.fd, &data, sizeof(DATA), 0);
            printf("innout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                   data.flags, data.num, data.name, data.text1, data.text2, data.text3);
            timeget(time);
            sprintf(sql, "insert into %s values('%s', '%s', '%s', '%s')", HDBNAME, time, data.name, "login", "N");
            if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
            {
                printf("录入数据库失败：%s\n", errmsg);
            }
            else
            {
                printf("操作已录入数据库\n");
            }
        }
    }
}

void sqlite_g(TEMP *a, DATA *b) //遍历库 查询行为
{
    TEMP temp = *a;
    DATA data = *b;
    int i;
    char name[32] = {0};
    char sql[128] = {0};
    char time[32] = {0};
    char *errmsg;
    char **dbResult;
    int nRow = 0, nColumn = 0;

    sprintf(sql, "select * from %s where name='%s' and power=%d", UDBNAME, data.name, 0);
    sqlite3_get_table(temp.db, sql, &dbResult, &nRow, &nColumn, &errmsg);
    if ((nRow != 0)) //普通用户只可查看自己信息 管理员可全部查看
    {
        if (data.flags == 4) //历史查询
        {
            memset(data.text1, 0, sizeof(data.text1));
            strcpy(data.text1, "n");
            send(temp.fd, &data, sizeof(DATA), 0);
            printf("40ngout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                   data.flags, data.num, data.name, data.text1, data.text2, data.text3);
            timeget(time);
            sprintf(sql, "insert into %s values('%s', '%s', '%s', '%s')", HDBNAME, time, data.name, "look", "power");
            if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
            {
                printf("录入数据库失败：%s\n", errmsg);
            }
            else
            {
                printf("操作已录入数据库\n");
            }
        }
        else
        {
            sprintf(sql, "select * from %s where name='%s' ", UDBNAME, data.name);
            sqlite3_get_table(temp.db, sql, &dbResult, &nRow, &nColumn, &errmsg);
            printf("30client:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                   data.flags, data.num, data.name, data.text1, data.text2, data.text3);
            strcpy(data.name, dbResult[nColumn]);
            strcpy(data.text1, dbResult[1 + nColumn]);
            strcpy(data.text2, dbResult[2 + nColumn]);
            strcpy(data.text3, dbResult[3 + nColumn]);
            data.num = (*dbResult[4 + nColumn]) - 48;
            strcpy(time, data.text3);
            send(temp.fd, &data, sizeof(DATA), 0);
            printf("30out:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                   data.flags, data.num, data.name, data.text1, data.text2, data.text3);
            recv(temp.fd, &data, sizeof(DATA), 0);
            data.flags = 0;
            send(temp.fd, &data, sizeof(DATA), 0);
            printf("30endout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                   data.flags, data.num, data.name, data.text1, data.text2, data.text3);

            timeget(time);
            sprintf(sql, "insert into %s values('%s', '%s', '%s', '%s')", HDBNAME, time, data.name, "look", "Y");
            if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
            {
                printf("录入数据库失败：%s\n", errmsg);
            }
            else
            {
                printf("操作已录入数据库\n");
            }
        }
    }
    else
    {
        if (data.flags == 4)
        {
            switch (data.num)
            {
            case 1:
                sprintf(sql, "select * from %s ", HDBNAME);
                sqlite3_get_table(temp.db, sql, &dbResult, &nRow, &nColumn, &errmsg);
                for (i = 1; i < nRow + 1; i++)
                {

                    strcpy(data.text1, dbResult[(nColumn * i)]);
                    strcpy(data.name, dbResult[1 + (nColumn * i)]);
                    strcpy(data.text2, dbResult[2 + (nColumn * i)]);
                    strcpy(data.text3, dbResult[3 + (nColumn * i)]);

                    strcpy(time, data.text3);
                    send(temp.fd, &data, sizeof(DATA), 0);
                    printf("41out:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                           data.flags, data.num, data.name, data.text1, data.text2, data.text3);
                    recv(temp.fd, &data, sizeof(DATA), 0);
                    if (strcmp(time, data.text1) != 0)
                    {
                        break;
                    }
                }
                break;
            case 2:
                sprintf(sql, "select * from %s where operate='%s' ", HDBNAME, data.text1);
                sqlite3_get_table(temp.db, sql, &dbResult, &nRow, &nColumn, &errmsg);
                for (i = 1; i < nRow + 1; i++)
                {

                    strcpy(data.text1, dbResult[(nColumn * i)]);
                    strcpy(data.name, dbResult[1 + (nColumn * i)]);
                    strcpy(data.text2, dbResult[2 + (nColumn * i)]);
                    strcpy(data.text3, dbResult[3 + (nColumn * i)]);

                    strcpy(time, data.text3);
                    send(temp.fd, &data, sizeof(DATA), 0);
                    printf("41out:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                           data.flags, data.num, data.name, data.text1, data.text2, data.text3);
                    recv(temp.fd, &data, sizeof(DATA), 0);
                    if (strcmp(time, data.text1) != 0)
                    {
                        break;
                    }
                }
                break;
            case 3:
                sprintf(sql, "select * from %s where operate='%s' and name='%s'", HDBNAME, data.text1, data.text2);
                sqlite3_get_table(temp.db, sql, &dbResult, &nRow, &nColumn, &errmsg);
                for (i = 1; i < nRow + 1; i++)
                {

                    strcpy(data.text1, dbResult[(nColumn * i)]);
                    strcpy(data.name, dbResult[1 + (nColumn * i)]);
                    strcpy(data.text2, dbResult[2 + (nColumn * i)]);
                    strcpy(data.text3, dbResult[3 + (nColumn * i)]);

                    strcpy(time, data.text3);
                    send(temp.fd, &data, sizeof(DATA), 0);
                    printf("41out:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                           data.flags, data.num, data.name, data.text1, data.text2, data.text3);
                    recv(temp.fd, &data, sizeof(DATA), 0);
                    if (strcmp(time, data.text1) != 0)
                    {
                        break;
                    }
                }
                break;
            case 4:
                sprintf(sql, "select * from %s where  name='%s'", HDBNAME, data.text2);
                sqlite3_get_table(temp.db, sql, &dbResult, &nRow, &nColumn, &errmsg);
                for (i = 1; i < nRow + 1; i++)
                {

                    strcpy(data.text1, dbResult[(nColumn * i)]);
                    strcpy(data.name, dbResult[1 + (nColumn * i)]);
                    strcpy(data.text2, dbResult[2 + (nColumn * i)]);
                    strcpy(data.text3, dbResult[3 + (nColumn * i)]);

                    strcpy(time, data.text3);
                    send(temp.fd, &data, sizeof(DATA), 0);
                    printf("41out:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                           data.flags, data.num, data.name, data.text1, data.text2, data.text3);
                    recv(temp.fd, &data, sizeof(DATA), 0);
                    if (strcmp(time, data.text1) != 0)
                    {
                        break;
                    }
                }
                break;
            }
            data.flags = 0;
            send(temp.fd, &data, sizeof(DATA), 0);
            printf("41endout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                   data.flags, data.num, data.name, data.text1, data.text2, data.text3);
            timeget(time);
            sprintf(sql, "insert into %s values('%s', '%s', '%s', '%s')", HDBNAME, time, data.name, "look", "Y");
            if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
            {
                printf("录入数据库失败：%s\n", errmsg);
            }
            else
            {
                printf("操作已录入数据库\n");
            }
        }
        else
        {
            sprintf(sql, "select * from %s where  power=%d", UDBNAME, 1);
            sqlite3_get_table(temp.db, sql, &dbResult, &nRow, &nColumn, &errmsg);
            for (i = 1; i < nRow + 1; i++)
            {

                strcpy(data.name, dbResult[(nColumn * i)]);
                strcpy(data.text1, dbResult[1 + (nColumn * i)]);
                strcpy(data.text2, dbResult[2 + (nColumn * i)]);
                strcpy(data.text3, dbResult[3 + (nColumn * i)]);
                data.num = (*dbResult[4 + (nColumn * i)]) - 48;
                strcpy(time, data.text3);
                send(temp.fd, &data, sizeof(DATA), 0);
                printf("31out:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                       data.flags, data.num, data.name, data.text1, data.text2, data.text3);
                recv(temp.fd, &data, sizeof(DATA), 0);
                if (strcmp(time, data.text1) != 0)
                {
                    break;
                }
            }
            sprintf(sql, "select * from %s where  power=%d", UDBNAME, 0);
            sqlite3_get_table(temp.db, sql, &dbResult, &nRow, &nColumn, &errmsg);
            for (i = 1; i < nRow + 1; i++)
            {
                strcpy(data.name, dbResult[(nColumn * i)]);
                strcpy(data.text1, dbResult[1 + (nColumn * i)]);
                strcpy(data.text2, dbResult[2 + (nColumn * i)]);
                strcpy(data.text3, dbResult[3 + (nColumn * i)]);
                data.num = (*dbResult[4 + (nColumn * i)]) - 48;
                strcpy(time, data.text3);
                send(temp.fd, &data, sizeof(DATA), 0);
                printf("31out:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                       data.flags, data.num, data.name, data.text1, data.text2, data.text3);
                recv(temp.fd, &data, sizeof(DATA), 0);
                if (strcmp(time, data.text1) != 0)
                {
                    break;
                }
            }
            data.flags = 0;
            send(temp.fd, &data, sizeof(DATA), 0);
            printf("31endout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                   data.flags, data.num, data.name, data.text1, data.text2, data.text3);
            timeget(time);
            sprintf(sql, "insert into %s values('%s', '%s', '%s', '%s')", HDBNAME, time, data.name, "look", "Y");
            if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
            {
                printf("录入数据库失败：%s\n", errmsg);
            }
            else
            {
                printf("操作已录入数据库\n");
            }
        }
    }
}

void sqlite_ch(TEMP *a, DATA *b) //修改员工信息
{
    TEMP temp = *a;
    DATA data = *b;
    char name[32] = {0};
    char sql[128] = {0};
    char time[32] = {0};
    char *errmsg;
    char **dbResult;
    int nRow = 0, nColumn = 0;
    sprintf(sql, "select * from %s where name='%s' and power=%d", UDBNAME, data.name, 0);
    sqlite3_get_table(temp.db, sql, &dbResult, &nRow, &nColumn, &errmsg);
    if (nRow != 0)
    {
        if (strcmp(data.name, data.text1) != 0)
        {
            memset(data.text1, 0, sizeof(data.text1));
            strcpy(data.text1, "n");
            send(temp.fd, &data, sizeof(DATA), 0);
            printf("0chnout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                   data.flags, data.num, data.name, data.text1, data.text2, data.text3);
            timeget(time);
            sprintf(sql, "insert into %s values('%s', '%s', '%s', '%s')", HDBNAME, time, data.name, "change", "power");
            if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
            {
                printf("录入数据库失败：%s\n", errmsg);
            }
            else
            {
                printf("操作已录入数据库\n");
            }
        }
        else
        {
            sprintf(sql, "update %s set %s='%s' where name='%s'", UDBNAME, data.text2, data.text3, data.text1);
            if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) == SQLITE_OK)
            {
                memset(data.text1, 0, sizeof(data.text1));
                strcpy(data.text1, "y");
                if (strcmp(data.text2, "name") == 0)
                {
                    strcpy(data.name, data.text3);
                }
                send(temp.fd, &data, sizeof(DATA), 0);
                printf("0chyout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                       data.flags, data.num, data.name, data.text1, data.text2, data.text3);
                timeget(time);
                sprintf(sql, "insert into %s values('%s', '%s', '%s', '%s')", HDBNAME, time, data.name, "change", "Y");
                if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
                {
                    printf("录入数据库失败：%s\n", errmsg);
                }
                else
                {
                    printf("操作已录入数据库\n");
                }
            }
        }
    }
    else
    {
        sprintf(sql, "update %s set %s='%s' where name='%s'", UDBNAME, data.text2, data.text3, data.text1);
        if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) == SQLITE_OK)
        {
            memset(data.text1, 0, sizeof(data.text1));
            strcpy(data.text1, "y");
            send(temp.fd, &data, sizeof(DATA), 0);
            if ((strcmp(data.name, data.text1) == 0) && (strcmp(data.text2, "name") == 0))
            {
                strcpy(data.name, data.text3);
            }
            printf("1chyout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                   data.flags, data.num, data.name, data.text1, data.text2, data.text3);
            timeget(time);
            sprintf(sql, "insert into %s values('%s', '%s', '%s', '%s')", HDBNAME, time, data.name, "change", "Y");
            if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
            {
                printf("录入数据库失败：%s\n", errmsg);
            }
            else
            {
                printf("操作已录入数据库\n");
            }
        }
        else
        {
            printf("%s\n", errmsg);
            memset(data.text1, 0, sizeof(data.text1));
            strcpy(data.text1, "n");
            send(temp.fd, &data, sizeof(DATA), 0);
            printf("1chnout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                   data.flags, data.num, data.name, data.text1, data.text2, data.text3);
            timeget(time);
            sprintf(sql, "insert into %s values('%s', '%s', '%s', '%s')", HDBNAME, time, data.name, "change", "N");
            if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
            {
                printf("录入数据库失败：%s\n", errmsg);
            }
            else
            {
                printf("操作已录入数据库\n");
            }
        }
    }
}

int sqlite_init(TEMP *a) //创建数据库表
{
    TEMP temp = *a;
    char sql[500] = {0}; //数据库操作字符串
    char *errmsg;        //数据库错误信息地址
    int ret;
    /*数据表有用户名 职位 薪资 */

    sprintf(sql, "create table %s(name text primary key, job text, salary text, password text, power int)", UDBNAME);

    if ((ret = sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg)) != SQLITE_OK)
    {
        if (ret != SQLITE_ERROR)
            return -1;
    }
    /*历史记录表有 时间 用户名 操作 结果*/
    sprintf(sql, "create table %s(time text,name text,operate text,return text)", HDBNAME);
    if ((ret = sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg)) != SQLITE_OK)
    {
        if (ret != SQLITE_ERROR)
            return -1;
    }
    sprintf(sql, "insert into  %s values('%s','%s','%s','%s',%d)",
            UDBNAME, "wzy", "WTO", "10W", "123456", 1);
    sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg);

    return 0;
}

void sqlite_del(TEMP *a, DATA *b) //删除员工信息
{
    TEMP temp = *a;
    DATA data = *b;
    char name[32] = {0};
    char sql[128] = {0};
    char time[32] = {0};
    char *errmsg;
    char **dbResult;
    int nRow = 0, nColumn = 0;
    sprintf(sql, "select * from %s where name='%s' and power=%d", UDBNAME, data.name, 0);
    sqlite3_get_table(temp.db, sql, &dbResult, &nRow, &nColumn, &errmsg);
    if (nRow != 0)
    {
        memset(data.text1, 0, sizeof(data.text1));
        strcpy(data.text1, "n");
        send(temp.fd, &data, sizeof(DATA), 0);
        printf("0dnout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
               data.flags, data.num, data.name, data.text1, data.text2, data.text3);
        timeget(time);
        sprintf(sql, "insert into %s values('%s', '%s', '%s', '%s')", HDBNAME, time, data.name, "delete", "power");
        if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
        {
            printf("录入数据库失败：%s\n", errmsg);
        }
        else
        {
            printf("操作已录入数据库\n");
        }
    }
    else
    {
        if (strcmp(data.text1, data.name) == 0)
        {
            memset(data.text1, 0, sizeof(data.text1));
            strcpy(data.text1, "n");
            send(temp.fd, &data, sizeof(DATA), 0);
            printf("1dnout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                   data.flags, data.num, data.name, data.text1, data.text2, data.text3);
            timeget(time);
            sprintf(sql, "insert into %s values('%s', '%s', '%s', '%s')", HDBNAME, time, data.name, "delete", "N");
            if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
            {
                printf("录入数据库失败：%s\n", errmsg);
            }
            else
            {
                printf("操作已录入数据库\n");
            }
        }
        sprintf(sql, "delete from %s where name='%s'", UDBNAME, data.text1);
        if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
        {
            printf("录入数据库失败：%s\n", errmsg);
            memset(data.text1, 0, sizeof(data.text1));
            strcpy(data.text1, "n");
            send(temp.fd, &data, sizeof(DATA), 0);
            printf("1dnout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                   data.flags, data.num, data.name, data.text1, data.text2, data.text3);
            timeget(time);
            sprintf(sql, "insert into %s values('%s', '%s', '%s', '%s')", HDBNAME, time, data.name, "delete", "N");
            if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
            {
                printf("录入数据库失败：%s\n", errmsg);
            }
            else
            {
                printf("操作已录入数据库\n");
            }
        }
        else
        {
            printf("成功：删除成功\n");
            memset(data.text1, 0, sizeof(data.text1));
            strcpy(data.text1, "y");
            send(temp.fd, &data, sizeof(DATA), 0);
            printf("1dyout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                   data.flags, data.num, data.name, data.text1, data.text2, data.text3);
            timeget(time);
            sprintf(sql, "insert into %s values('%s', '%s', '%s', '%s')", HDBNAME, time, data.name, "delete", "Y");
            if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
            {
                printf("录入数据库失败：%s\n", errmsg);
            }
            else
            {
                printf("操作已录入数据库\n");
            }
        }
    }
}

int sqlite_hes_del(TEMP *a, DATA *b) //删除历史记录
{
    TEMP temp = *a;
    DATA data = *b;
    char sql[128] = {0};
    char time[32] = {0};
    char *errmsg;
    int ret;
    char **dbResult;
    int nRow = 0, nColumn = 0;

    sprintf(sql, "select * from %s where name='%s' and power=%d", UDBNAME, data.name, 0);
    sqlite3_get_table(temp.db, sql, &dbResult, &nRow, &nColumn, &errmsg);
    if (nRow != 0) //无权限
    {
        memset(data.text1, 0, sizeof(data.text1));
        strcpy(data.text1, "n");
        send(temp.fd, &data, sizeof(DATA), 0);
        printf("delhesnout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
               data.flags, data.num, data.name, data.text1, data.text2, data.text3);
        timeget(time);
        sprintf(sql, "insert into %s values('%s', '%s', '%s', '%s')", HDBNAME, time, data.name, "deletehes", "power");
        if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
        {
            printf("录入数据库失败：%s\n", errmsg);
        }
        else
        {
            printf("操作已录入数据库\n");
        }
    }
    else //有权限
    {
        sprintf(sql, "drop  table  %s", HDBNAME);
        if ((ret = sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg)) != SQLITE_OK)
        {
            memset(data.text1, 0, sizeof(data.text1));
            strcpy(data.text1, "n");
            send(temp.fd, &data, sizeof(DATA), 0);
            printf("delhesnout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                   data.flags, data.num, data.name, data.text1, data.text2, data.text3);
            timeget(time);
            sprintf(sql, "insert into %s values('%s', '%s', '%s', '%s')", HDBNAME, time, data.name, "deletehes", "N");
            if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
            {
                printf("录入数据库失败：%s\n", errmsg);
            }
            else
            {
                printf("操作已录入数据库\n");
            }
        }
        else
        {
            memset(data.text1, 0, sizeof(data.text1));
            strcpy(data.text1, "y");
            send(temp.fd, &data, sizeof(DATA), 0);
            printf("delhesyout:flags=%d,num=%d,name=%s,text1=%s,text2=%s,text3=%s\n",
                   data.flags, data.num, data.name, data.text1, data.text2, data.text3);
            sprintf(sql, "create table %s(time text,name text,operate text,return text)", HDBNAME);
            if ((ret = sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg)) != SQLITE_OK)
            {
                if (ret != SQLITE_ERROR)
                {
                    return sqlite_init(a);
                }
            }
            timeget(time);
            sprintf(sql, "insert into %s values('%s', '%s', '%s', '%s')", HDBNAME, time, data.name, "deletehes", "Y");
            if (sqlite3_exec(temp.db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
            {
                printf("录入数据库失败：%s\n", errmsg);
            }
            else
            {
                printf("操作已录入数据库\n");
            }
        }
    }
    return 0;
}

void timeget(char *ret) //用于获取当前时间
{

    time_t mytime;

    time(&mytime);

    struct tm *mytm = localtime(&mytime);
    sprintf(ret, "%04d/%02d/%02d %02d:%02d:%02d\n", mytm->tm_year + 1900,
            mytm->tm_mon + 1, mytm->tm_mday, mytm->tm_hour, mytm->tm_min, mytm->tm_sec);
}